import request from '@/utils/request'

// 用户类型定义
export interface User {
  id: string
  username: string
  email: string
  role: string
  status: 'active' | 'inactive'
  createdAt: string
  updatedAt: string
}

// 用户查询参数
export interface UserQuery {
  page?: number
  pageSize?: number
  keyword?: string | null
  role?: string | null
  status?: 'active' | 'inactive' | null
  sortBy?: string
  sortOrder?: 'asc' | 'desc'
}

// 创建用户
export const createUser = (userData: Omit<User, 'id' | 'createdAt' | 'updatedAt'>) => {
  return request.post<User>('/users', userData)
}

// 获取用户列表
export const getUsers = (query?: UserQuery) => {
  return request.get<{ list: User[]; total: number }>('/users', { params: query }) as unknown as { list: User[]; total: number }
}

// 获取单个用户
export const getUser = (id: string) => {
  return request.get<User>(`/users/${id}`)
}

// 更新用户
export const updateUser = (
  id: string,
  userData: Partial<Omit<User, 'id' | 'createdAt' | 'updatedAt'>>,
) => {
  return request.put<User>(`/users/${id}`, userData)
}

// 删除用户
export const deleteUser = (id: string) => {
  return request.delete(`/users/${id}`) as unknown as void
}

// 批量删除用户
export const batchDeleteUsers = (ids: string[]) => {
  return request.delete('/users/batch', { data: { ids } }) as unknown as void
}

// 重置用户密码
export const resetPassword = (id: string, newPassword: string) => {
  return request.post(`/users/${id}/reset-password`, { newPassword }) as unknown as void
}
